


float4x4 g_projMatrix : PROJ;
float4x4 g_viewMatrix : VIEW;
float4x4 g_worldMatrix : WORLD;

float4 g_lightAmbient : LIGHTAMBIENT;
float4 g_lightDiffuse : LIGHTDIFFUSE;
float4 g_lightSpecular : LIGHTSPECULAR;
float3 g_lightPosition : LIGHTPOSITION;

float g_matPower : MATERIALPOWER;

texture g_texDiffuse : TEXTUREDIFFUSE;
texture g_texSpecular : TEXTURESPECULAR;
texture g_texBump : TEXTUREBUMP;

sampler2D g_samDiffuse = 
sampler_state
{
	Texture = <g_texDiffuse>;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Anisotropic;
	MaxAnisotropy = 16.0f;
};

sampler2D g_samSpecular = 
sampler_state
{
	Texture = <g_texSpecular>;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Anisotropic;
	MaxAnisotropy = 16.0f;
};

sampler2D g_samBump = 
sampler_state
{
	Texture = <g_texBump>;
	MinFilter = Anisotropic;
	MagFilter = Anisotropic;
	MipFilter = Anisotropic;
	MaxAnisotropy = 16.0f;
};

struct VS_OUTPUT
{
	float4 position		: POSITION;
	float2 txt			: TEXCOORD0;
	float3 lightDir		: TEXCOORD1;
	float3 normal		: TEXCOORD2;
	float3 eyeDir		: TEXCOORD3;
	float3 tangent		: TEXCOORD4;
	float3 bitangent	: TEXCOORD5;
	float2 tangentScale	: TEXCOORD6;
};

struct PS_OUTPUT
{
	float4 color : COLOR0;
};

VS_OUTPUT MyVS(float4	pos		: POSITION,
			   float4   normal	: NORMAL,
			   float2   txt		: TEXCOORD0,
			   uniform float4x4 worldViewMatrix,
			   uniform float4x4 worldViewProjMatrix)
{
	VS_OUTPUT output;

	output.position = mul (pos, worldViewProjMatrix);

	float3 position = mul (pos, worldViewMatrix).xyz;
	output.normal = mul (float4(normal.xyz, 0.0), worldViewMatrix).xyz;

	output.lightDir = mul(float4(g_lightPosition, 1.0), g_viewMatrix).xyz - position;
	output.txt = txt;
	output.eyeDir = position;
	output.tangent = mul (float4 (1.0, 0.0, 0.0, 0.0), worldViewMatrix).xyz;
	output.bitangent = mul (float4 (0, 0, 1, 0), worldViewMatrix).xyz;
	output.tangentScale = float2 (200.0f, 200.0f);
	return output;
}


PS_OUTPUT MyPS(VS_OUTPUT input)
{
	PS_OUTPUT output;
	output.color = float4 (0, 0, 0, 0);
	float4 bump = tex2D (g_samBump, input.txt);
	float4 diff = tex2D (g_samDiffuse, input.txt);
	float4 spec = tex2D (g_samSpecular, input.txt);

	float3x3 loc = transpose (float3x3 (normalize (input.tangent), normalize (input.bitangent), normalize (input.normal)));
	float3 locLight = normalize (mul (input.lightDir, loc));
	float3 locEye = normalize (mul (input.eyeDir, loc));

	float3 locNormal = normalize (bump.xyz * float3(2, 2, 2) - float3 (1, 1, 1));


	float d = dot (locNormal, locLight);
	if (d > 0.0)
	{
		output.color = output.color + g_lightDiffuse * d;
	}

	float3 ref = reflect (locLight, locNormal);
	float s = pow (max (dot (ref, locEye), 0.0), g_matPower);
	if (s > 0.0)
	{
		output.color = output.color + spec * g_lightSpecular * s;
	}
	
	output.color = output.color * diff;
	return output;
}

technique TestTechnique
{
	pass P0
	{
		VertexShader = compile vs_2_0 MyVS(mul (g_worldMatrix, g_viewMatrix), mul(g_worldMatrix, mul (g_viewMatrix, g_projMatrix)));
		PixelShader  = compile ps_2_0 MyPS();
	}
}